package net.xju.controller;

import net.xju.common.framework.simplemvc.RequestMapping;
import net.xju.common.utils.JSONUtil;
import net.xju.common.utils.Result;
import net.xju.entity.Consultant;
import net.xju.entity.User;
import net.xju.entity.Admin;
import net.xju.service.ConsultantService;
import net.xju.service.UserService;
import net.xju.service.AdminService;
import net.xju.service.impl.ConsultantServiceImpl;
import net.xju.service.impl.UserServiceImpl;
import net.xju.service.impl.AdminServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;

@RequestMapping("/api")
public class AdminController {
    private ConsultantService consultantService = new ConsultantServiceImpl();
    private UserService userService = new UserServiceImpl();
    private AdminService adminService = new AdminServiceImpl();

    @RequestMapping("/consultant/list.do")
    public void consultantList(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        JSONUtil.printByJSON(resp, Result.success(consultantService.list()));
    }

    @RequestMapping("/consultant/insert.do")
    public void consultantInsert(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        Consultant consultant = new Consultant();
        consultant.setUsername(req.getParameter("username"));
        consultant.setPassword(req.getParameter("password"));
        consultant.setAge(Integer.parseInt(req.getParameter("age")));
        consultant.setGender(req.getParameter("gender"));
        if (consultantService.insert(consultant)) {
            JSONUtil.printByJSON(resp, Result.success("咨询师添加成功"));
        } else {
            JSONUtil.printByJSON(resp, Result.fail("咨询师添加失败"));
        }
    }

    @RequestMapping("/consultant/update.do")
    public void consultantUpdate(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        Consultant consultant = new Consultant();
        consultant.setId(Integer.parseInt(req.getParameter("id")));
        consultant.setUsername(req.getParameter("username"));
        consultant.setPassword(req.getParameter("password"));
        consultant.setAge(Integer.parseInt(req.getParameter("age")));
        consultant.setGender(req.getParameter("gender"));
        if (consultantService.update(consultant)) {
            JSONUtil.printByJSON(resp, Result.success("咨询师更新成功"));
        } else {
            JSONUtil.printByJSON(resp, Result.fail("咨询师更新失败"));
        }
    }

    @RequestMapping("/consultant/deleteById.do")
    public void consultantDelete(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        int id = Integer.parseInt(req.getParameter("id"));
        if (consultantService.delete(id)) {
            JSONUtil.printByJSON(resp, Result.success("咨询师删除成功"));
        } else {
            JSONUtil.printByJSON(resp, Result.fail("咨询师删除失败"));
        }
    }

    @RequestMapping("/user/list.do")
    public void userList(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        JSONUtil.printByJSON(resp, Result.success(userService.list()));
    }

    @RequestMapping("/user/insert.do")
    public void userInsert(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        User user = new User();
        user.setUsername(req.getParameter("username"));
        user.setPassword(req.getParameter("password"));
        user.setAge(Integer.parseInt(req.getParameter("age")));
        user.setGender(req.getParameter("gender"));
        if (userService.insert(user)) {
            JSONUtil.printByJSON(resp, Result.success("用户添加成功"));
        } else {
            JSONUtil.printByJSON(resp, Result.fail("用户添加失败"));
        }
    }

    @RequestMapping("/user/update.do")
    public void userUpdate(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        User user = new User();
        user.setId(Integer.parseInt(req.getParameter("id")));
        user.setUsername(req.getParameter("username"));
        user.setPassword(req.getParameter("password"));
        user.setAge(Integer.parseInt(req.getParameter("age")));
        user.setGender(req.getParameter("gender"));
        if (userService.update(user)) {
            JSONUtil.printByJSON(resp, Result.success("用户更新成功"));
        } else {
            JSONUtil.printByJSON(resp, Result.fail("用户更新失败"));
        }
    }

    @RequestMapping("/user/deleteById.do")
    public void userDelete(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        int id = Integer.parseInt(req.getParameter("id"));
        if (userService.delete(id)) {
            JSONUtil.printByJSON(resp, Result.success("用户删除成功"));
        } else {
            JSONUtil.printByJSON(resp, Result.fail("用户删除失败"));
        }
    }

    @RequestMapping("/admin/update.do")
    public void adminUpdate(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        Admin admin = new Admin();
        admin.setId(Integer.parseInt(req.getParameter("id")));
        admin.setUsername(req.getParameter("username")); // 虽然不可改，但要传递
        admin.setPassword(req.getParameter("password"));
        admin.setAge(Integer.parseInt(req.getParameter("age")));
        admin.setGender(req.getParameter("gender"));
        if (adminService.update(admin)) {
            JSONUtil.printByJSON(resp, Result.success("管理员更新成功"));
        } else {
            JSONUtil.printByJSON(resp, Result.fail("管理员更新失败"));
        }
    }
}